home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / turtle.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  242 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    turtle -
  19.  *         A simple implementation of turtle graphics.
  20.  *
  21.  *                Paul Haeberli - 1984
  22.  *
  23.  */
  24. #include "values.h"
  25. #include "math.h"
  26. #include "gl.h"
  27.  
  28. #define RADIANS(a) ((a)*(M_PI/180.0))
  29. #define INVISIBLE -1
  30. #define NONE -1
  31.  
  32. static drawline();
  33. static tg_addpolypoint();
  34.  
  35.  
  36. static float lastx = 10000,
  37.          lasty = 10000,
  38.          lastz = 10000;
  39.  
  40. static float xposition = 0.0;
  41. static float yposition = 0.0;
  42. static float zposition = 0.0;
  43. static float uposition = 0.0;
  44. static float vposition = 0.0;
  45. static float heading = 0.0;
  46. static int  penc = INVISIBLE;
  47. static int  pent = 0;
  48. static int  fillc = INVISIBLE;
  49. static int  fillt = 0;
  50. static int  tgpolypoints = 0;
  51. float tgpoly[1000][3];
  52.  
  53. /*
  54.  *    CLEARSCREEN - erase the entire screen area.
  55.  */
  56. CLEARSCREEN()
  57. {
  58.     clear();
  59. }
  60.  
  61. /*
  62.  *    MOVE - move some distance.
  63.  */
  64. MOVE( distance )
  65. float distance;
  66. {
  67.     float x, y;
  68.  
  69.     x = uposition + distance * cos( RADIANS( heading ) );
  70.     y = vposition + distance * sin( RADIANS( heading ) );
  71.     MOVETO( x, y );
  72. }
  73.  
  74. /*
  75.  *    MOVETO - move to a cartesian coordinate.
  76.  */
  77. MOVETO(x,y)
  78. float x, y;
  79. {
  80.     if (penc != INVISIBLE)
  81.     drawline(uposition,vposition,0.0,x,y,0.0);
  82.     uposition = x;
  83.     vposition = y;
  84.     tg_addpolypoint(x,y,0.0); 
  85. }
  86.  
  87. /*
  88.  *    SPACETO - move to a point in 3 space.
  89.  */
  90. SPACETO( x, y, z )
  91. float x, y, z;
  92. {
  93.     if (penc != INVISIBLE)
  94.     drawline(xposition,yposition,zposition,x,y,z);
  95.     xposition = x;
  96.     yposition = y;
  97.     zposition = z;
  98.     tg_addpolypoint(x,y,z);
  99. }
  100.  
  101. /*
  102.  *    TURN - turn the turtle.
  103.  */
  104. TURN(angle)
  105. float    angle;
  106. {
  107.     heading =  heading + angle;
  108.     heading -= 360 * (int)( heading / 360.0 );
  109. }
  110.  
  111. /*
  112.  *    TURNTO - turn to a specific angle.
  113.  */
  114. TURNTO(angle)
  115. float    angle;
  116. {
  117.     heading =  angle;
  118.     heading -= 360 * (int)(heading / 360.0);
  119. }
  120.  
  121. /*
  122.  *    FILLCOLOR 
  123.  *    FILLTEXTURE
  124.  *    PENCOLOR
  125.  *    PENTYPE - change the fillcolor, filltexture, pencolor, and pentype.
  126.  */
  127. FILLCOLOR( fillcolor )
  128. int fillcolor;
  129. {
  130.     fillc = fillcolor;
  131. }
  132.  
  133. FILLTEXTURE( filltype )
  134. int filltype;
  135. {
  136.     fillt = filltype;
  137. }
  138.  
  139. PENCOLOR( threadcolor )
  140. int threadcolor;
  141. {
  142.     penc = threadcolor;
  143.     if (penc != INVISIBLE)
  144.     color(penc); 
  145. }
  146.  
  147. PENTYPE(threadtype)
  148. int threadtype;
  149. {
  150.     pent = threadtype;
  151. }
  152.  
  153. /*
  154.  *    WHATCOLOR - return the current color.    
  155.  */
  156. WHATCOLOR()
  157. {
  158.     return penc;
  159. }
  160.  
  161. /*
  162.  *    WHEREAMI - return our current heading and position.
  163.  */
  164. WHEREAMI(x,y,h)
  165. float *x,*y;
  166. float *h;
  167. {
  168.     *x = uposition;
  169.     *y = vposition;
  170.     *h = heading;
  171. }
  172.  
  173. /*
  174.  *    BEGINPOLY - start a polygon.
  175.  */
  176. BEGINPOLY()
  177. {
  178.     tgpolypoints = 0; 
  179. }
  180.  
  181. /*
  182.  *    ENDPOLY - close and fill a polygon.
  183.  */
  184. ENDPOLY()
  185. {
  186.     int i;
  187.  
  188.     bgnpolygon();
  189.     for(i=0; i<tgpolypoints; i++) 
  190.     v3f(tgpoly[i]);
  191.     endpolygon();
  192. }
  193.  
  194. /*
  195.  *    TEXT - put some text.
  196.  */
  197. TEXT( x, y, string )
  198. float x, y;
  199. char *string;
  200. {
  201.     cmov2( x, y );
  202.     charstr( string );
  203. }
  204.  
  205. /*
  206.  *    VIEWPORT - set the viewport.
  207.  */
  208. VIEWPORT(x1,y1,x2,y2)
  209. float x1, x2, y1, y2;
  210. {
  211.     viewport(x1,y1,x2,y2);
  212. }
  213.  
  214. /*
  215.  *    WINDOW - set the window.
  216.  */
  217. WINDOW(x1,y1,x2,y2)
  218. float x1, x2, y1, y2;
  219. {
  220.     ortho2(x1,y1,x2,y2); 
  221. }
  222.  
  223. static drawline(x1,y1,z1,x2,y2,z2)
  224. float x1, y1, z1, x2, y2, z2;
  225. {
  226.     if ((x1 != lastx) || (y1 != lasty) || (z1 != lastz))
  227.         move(x1,y1,z1);
  228.     draw(x2,y2,z2);
  229.     lastx = x2;
  230.     lasty = y2;
  231.     lastz = z2;
  232. }
  233.  
  234. static tg_addpolypoint(x,y,z)
  235. float x, y, z;
  236. {
  237.     tgpoly[tgpolypoints][0] = x;
  238.     tgpoly[tgpolypoints][1] = y;
  239.     tgpoly[tgpolypoints][2] = z;
  240.     tgpolypoints = (tgpolypoints+1) % 1000;
  241. }
  242.